package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.lepu.app.fun.index.activity.IndexBookContentActivity;
import com.lepu.app.fun.index.bean.BeanIndexBookItem;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.pasm.R;

public class IndexBookItemAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater = null;
	private Context context = null;
	private ArrayList<BeanIndexBookItem> bookList;
	private int bookIndex; //第几本书
	
	public IndexBookItemAdapter(Context context , ArrayList<BeanIndexBookItem> bookList, int bookIndex) {
		this.context = context;
		this.bookList = bookList;
		this.bookIndex = bookIndex;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		
		return bookList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.index_book_item_item, null);

			holder = new ViewHolder();
			TextView chapter = (TextView) convertView.findViewById(R.id.chapter);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			RelativeLayout itemLayout = (RelativeLayout) convertView.findViewById(R.id.itemLayout);

			holder.chapter = chapter;
			holder.title = title;
			holder.itemLayout = itemLayout;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
        
		holder.chapter.setText(bookList.get(position).chapter);
		holder.title.setText(bookList.get(position).title);
		
		if (BeanIndexNew.isExist(bookList.get(position).title)) {
			holder.chapter.setTextColor(context.getResources().getColor(R.color.color_text_black2));
			holder.title.setTextColor(context.getResources().getColor(R.color.color_text_black2));
		}else{
			holder.chapter.setTextColor(context.getResources().getColor(R.color.color_text_black));
			holder.title.setTextColor(context.getResources().getColor(R.color.color_text_black));
		}
		holder.itemLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intentRead = new Intent(context, IndexBookContentActivity.class);
				intentRead.putExtra("book", bookIndex);
				intentRead.putExtra("index", position);
				((BaseFragmentActivity)context).startActivity(intentRead,true);
				
				BeanIndexNew.insertTargetId(bookList.get(position).title);
				holder.chapter.setTextColor(context.getResources().getColor(R.color.color_text_black2));
				holder.title.setTextColor(context.getResources().getColor(R.color.color_text_black2));
			}
		});

		return convertView;
	}
	
	static class ViewHolder {
		TextView chapter;
		TextView title;
		RelativeLayout itemLayout;
	}
}
